import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class FireRuneCrafter extends Script implements PaintListener {

	public long startTime = System.currentTimeMillis();

	int Ringcount;
	int Ring;
	int loads;

	private int startLvl = 0;
	private int startExp = 0;

	int runAt = random(39, 100);

	RSTile[] RuinPath = { new RSTile(3310, 3234), new RSTile(3307, 3245),
			new RSTile(3311, 3254) };

	public boolean atAltar() {

		final int nameX[] = new int[] { 2595, 2574, 2578, 2593 };
		final int nameY[] = new int[] { 4845, 4850, 4830, 4833 };
		final Polygon name = new Polygon(nameX, nameY, 4);
		if (name.contains(getMyPlayer().getLocation().getX(), getMyPlayer()
				.getLocation().getY())) {
			return true;
		}
		return false;
	}

	public boolean atCastelWars() {

		final int nameX[] = new int[] { 2434, 2435, 2458, 2459 };
		final int nameY[] = new int[] { 3102, 3081, 3076, 3100 };
		final Polygon name = new Polygon(nameX, nameY, 4);
		if (name.contains(getMyPlayer().getLocation().getX(), getMyPlayer()
				.getLocation().getY())) {
			return true;
		}
		return false;
	}

	public boolean atDesert() {

		final int nameX[] = new int[] { 3326, 3301, 3296, 3321 };
		final int nameY[] = new int[] { 3224, 3225, 3263, 3264 };
		final Polygon name = new Polygon(nameX, nameY, 4);
		if (name.contains(getMyPlayer().getLocation().getX(), getMyPlayer()
				.getLocation().getY())) {
			return true;
		}
		return false;
	}

	public boolean atRuins() {

		final int nameX[] = new int[] { 3307, 3320, 3319, 3306 };
		final int nameY[] = new int[] { 3250, 3249, 3259, 3261 };
		final Polygon name = new Polygon(nameX, nameY, 4);
		if (name.contains(getMyPlayer().getLocation().getX(), getMyPlayer()
				.getLocation().getY())) {
			return true;
		}
		return false;
	}

	public boolean clickInventoryItem(final int i, final boolean flag) {
		if (getCurrentTab() != 3 && !RSInterface.getInterface(762).isValid()
				&& !RSInterface.getInterface(620).isValid()) {
			openTab(3);
		}
		final int ai[] = getInventoryArray();
		final ArrayList<Integer> arraylist = new ArrayList<Integer>();
		for (int j = 0; j < ai.length; j++) {
			if (ai[j] == i) {
				arraylist.add(Integer.valueOf(j));
			}
		}

		if (arraylist.size() == 0) {
			return false;
		} else {
			final int k = arraylist.get(random(0, arraylist.size())).intValue();
			final Point point = getInventoryItemPoint(k);
			clickMouse(point, 5, 5, flag);
			return true;
		}
	}

	public boolean clickTile(final RSTile tile, final String action) {
		try {
			final Point location = Calculations.tileToScreen(tile);
			if (location.x == -1 || location.y == -1) {
				return false;
			}
			moveMouse(location, 10, 10);

			clickMouse(true);
			wait(random(500, 1000));
			while (true) {
				if (!getMyPlayer().isMoving()) {
					break;
				}
				wait(random(500, 1000));
			}
			return true;
		} catch (final Exception e) {
			return false;
		}
	}

	@Override
	public String getAuthor() {
		return "Hellfish";
	}

	// METHODS---METHODS---METHODS---METHODS

	@Override
	public String getName() {
		return "Hellfish";
	}

	@Override
	public String getScriptCategory() {
		return "Runecraft";
	}

	@Override
	public String getScriptDescription() {
		return "<html>\n" + "<head></head>\n"
				+ "<body bgcolor=\"black\" text=\"white\">\n" + "<center>"
				+ "<h2>" + getName() + "</h2>" + "</center>\n"
				+ "<b>Author:</b> " + getAuthor() + "<br />"
				+ "<b>Version:</b> " + getVersion() + "<BR>Script nice"
				+ "</body>\n" + "</html>";
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public int loop() {
		if (getEnergy() >= runAt && !isRunning()) {

			clickMouse(727 + random(3, 3), 106 + random(2, 3), false);
			atMenu("on");
			wait(200);
			runAt = random(39, 100);
			return random(39, 50);
		}

		if (getMyPlayer().isMoving()) {

			return random(100, 150);
		}

		if (inventoryContains(1436)) {
			// Teleporting to Al-kharid

			if (atCastelWars()) {

				if (inventoryContains(2552)) {
					clickInventoryItem(2552, true);
					log("Equipped new ring!");
				}
				if (getCurrentTab() != Constants.TAB_EQUIPMENT) {
					openTab(Constants.TAB_EQUIPMENT);

				} else if (getCurrentTab() == Constants.TAB_EQUIPMENT) {
					clickMouse(701 + random(2, 0), 386 + random(1, 2), false);
					clickMouse(706 + random(2, 0), 428 + random(1, 2), true);
					wait(1300);
					if (RSInterface.getInterface(232).isValid()) {
						clickMouse(275 + random(2, 4), 388 + random(4, 3), true);
						log("Teleporting to Al-Kharid.");
						Ringcount++;
						wait(10000);
					}
				}
			}

			// Teleporting ended, we should now be at Al-Kharid.

			// Going trough ruins
			if (atDesert()) {
				if (!atRuins()) {
					walkPathMM(randomizePath(RuinPath, 2, 2), 14);

				} else {
					final RSTile AltarTile = new RSTile(3313, 3255);
					clickTile(AltarTile, "Enter");
					log("Attempting to click at RuinAltar.");
					wait(500);
				}
			}
			// From now of we are at altar. :)
			if (atAltar()) {
				if (distanceTo(new RSTile(2576, 4847)) <= 5) {
					walkTileMM(new RSTile(2585, 4838));
				} else {
					final RSTile RuinTile = new RSTile(2585, 4838);
					log("clicking Altar.");
					clickTile(RuinTile, "Enter");

					wait(1200);

				}
			}

		} else {
			// Have now gotten our fire runes =) =) =)

			if (atAltar()) {

				if (getCurrentTab() != Constants.TAB_EQUIPMENT) {
					openTab(Constants.TAB_EQUIPMENT);

				} else if (getCurrentTab() == Constants.TAB_EQUIPMENT) {
					clickMouse(701 + random(2, 0), 386 + random(1, 2), false);
					clickMouse(706 + random(2, 0), 428 + random(1, 2), true);
					wait(1300);
					if (RSInterface.getInterface(232).isValid()) {
						clickMouse(254 + random(2, 4), 409 + random(4, 3), true);
						log("Teleporting to CastelWars.");
						Ringcount++;

						wait(10000);
					}
				}
			}

			if (atCastelWars()) {

				if (distanceTo(new RSTile(2442, 3092)) <= 5) {
					log("Attempting to bank...");
					walkTileMM(new RSTile(2445, 3084));
					wait(1000);
				}

				final RSTile BankTile = new RSTile(2445, 3083);
				atTile(BankTile, "Use");
				wait(1400);

				if (RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()) {

					if (Ringcount == 8) {
						log("Withdrawing a new ring!");

						clickMouse(399 + random(2, 4), 307 + random(3, 5), true);

						clickMouse(99 + random(2, 4), 102 + random(3, 5), true);

						clickMouse(51 + random(0, 2), 107 + random(0, 0), false);
						wait(20);
						clickMouse(72 + random(2, 1), 177 + random(2, 3), true);
						clickMouse(489 + random(3, 3), 33 + random(3, 3), true);
						Ringcount = 0;
						log("Got a new ring!");
						Ring++;
					} else {

						clickMouse(399 + random(2, 4), 307 + random(3, 5), true);
						clickMouse(51 + random(0, 2), 107 + random(0, 0), false);
						wait(20);
						clickMouse(72 + random(2, 1), 177 + random(2, 3), true);
						clickMouse(489 + random(3, 3), 33 + random(3, 3), true);
						log("Withdrawed new esses!");
						loads++;
					}
				}
			}

		}
		return random(100, 150);
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {

		long millis = System.currentTimeMillis() - startTime;
		final long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		final long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		if (startExp == 0) {
			startExp = skills.getCurrentSkillExp(20);
		}
		if (startLvl == 0) {
			startLvl = skills.getCurrentSkillLevel(20);
		}

		g.setColor(Color.green);

		g.setColor(Color.YELLOW);
		g.drawString("Hellfish's FireRune crafter v1.0", 320, 260);
		g.setColor(Color.RED);
		g.drawString("Run: " + hours + " hrs " + minutes + " min ", 320, 275);
		g.drawString("Rings used: " + Ring, 320, 290);
		g.drawString("Trips: " + loads, 320, 305);

		g.drawString("Levels Gained: "
				+ (skills.getCurrentSkillLevel(20) - startLvl), 50, 260);
		g.drawString("Experience Earned: "
				+ (skills.getCurrentSkillExp(20) - startExp), 50, 275);
		g.drawString("Current Level: " + skills.getCurrentSkillLevel(20), 50,
				290);
		g.drawString("Exp to next level: " + skills.getXPToNextLevel(20), 50,
				305);

	}

	// METHODS---METHODS---METHODS---METHODS

	@Override
	public boolean onStart(final Map<String, String> args) {

		if (equipmentContains(2552)) {

			log("We have 8 teleports left in ring, means that we will start correctly.");
		}
		if (equipmentContains(2554)) {
			log("Please equipp a new ring with 8 charges.");
			stopAllScripts();
		}
		if (equipmentContains(2556)) {
			log("Please equipp a new ring with 8 charges.");
			stopAllScripts();
		}
		if (equipmentContains(2558)) {
			log("Please equipp a new ring with 8 charges.");
			stopAllScripts();
		}
		if (equipmentContains(2560)) {
			log("Please equipp a new ring with 8 charges");
			stopAllScripts();
		}
		if (equipmentContains(2562)) {
			log("Please equipp a new ring with 8 charges.");
			stopAllScripts();
		}
		if (equipmentContains(2564)) {
			log("Please equipp a new ring with 8 charges.");
			stopAllScripts();
		}
		if (equipmentContains(2566)) {

			log("Please equipp a new ring with 8 charges.");
			stopAllScripts();
		}

		return true;
	}

}